"""


"""
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from meiduo_admin.my_pagination import MyPageNumberPagination

from meiduo_admin.serializers import SKU_Serializers


class SKUListView(ModelViewSet):

    def get_queryset(self):

        keyword = self.request.query_params.get("keyword")

        if not keyword:
            return SKU.objects.all().order_by("-id")

        else:
            return SKU.objects.filter(name__contains=keyword).order_by("-id")

    pagination_class = MyPageNumberPagination

    serializer_class = SKU_Serializers.SKUSerializer


# # 二级视图加上modelmixin方法编写
# class SKUListView(GenericAPIView, ListModelMixin):
#     serializer_class = serializer.SKUSerializer
#
#     pagination_class = MyPageNumberPagination
#
#     def get_queryset(self):
#
#         keyword = self.request.query_params.get("keyword")
#
#         if keyword:
#             return SKU.objects.filter(name__contains=keyword).order_by("-id")
#
#         else:
#             return SKU.objects.all().order_by("-id")
#
#     def get(self, request):
#
#         return self.list(request)


class SKUCategoryView(ListAPIView):

    queryset = GoodsCategory.objects.filter(subs=None)

    serializer_class = SKU_Serializers.SKUCategorySerializer

# 二级视图加listmodelmixin编写
# class SKUCategoryView(GenericAPIView, ListModelMixin):
#
#     queryset = GoodsCategory.objects.filter(subs=None)
#
#     serializer_class = serializer.SKUCategorySerializer
#
#     def get(self, request):
#         return self.list(request)


class SPUCategoryView(ListAPIView):

    queryset = SPU.objects.all()

    serializer_class = SKU_Serializers.SPUCategorySerializer

# 一级视图的编写
# class SPUCategoryView(APIView):
#
#     def get(self, request):
#
#         queryset = SPU.objects.all()
#
#         myserializer = serializer.SPUCategorySerializer(instance=queryset, many=True)
#
#         return Response(myserializer.data)


class SPUSpecs(ListAPIView):

    def get_queryset(self):

        pk = self.kwargs.get("pk")

        SPU_query = SPUSpecification.objects.filter(spu_id=pk)

        return SPU_query

    serializer_class = SKU_Serializers.SPUSpecsSerializer
